package ppackage;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class picture {
    public static void main(String[] args) {
        try {
            // 使用绝对路径
            String filePath = "D:\\CodeFiles\\JAVA\\Experiment\\Experiment04\\Experiment403picture\\pmodule\\src\\resources\\fd.jpg";
            File file = new File(filePath);
            if (!file.exists()) {
                System.out.println("文件不存在: " + file.getAbsolutePath());
                return;
            }

            BufferedImage image = ImageIO.read(file);
            if (image == null) {
                System.out.println("无法读取图像文件: " + file.getAbsolutePath());
                return;
            }

            Graphics g = image.getGraphics();
            g.setColor(Color.red);
            g.drawChars("By Michael".toCharArray(), 0, 10, 10, 10);
            g.setColor(Color.GREEN);
            g.drawChars("我处理的图片".toCharArray(), 0, "我处理的图片".length(), image.getWidth() - 80, image.getHeight() - 10);

            JFrame jm = new JFrame();
            jm.setBounds(500, 200, 200, 200);
            ImageIcon ic = new ImageIcon(image);
            JPanel panel = new JPanel();
            JLabel jl = new JLabel(ic);
            panel.add(jl);
            jm.add(panel);
            jm.setVisible(true);
            jm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
